package marvin.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;
import javax.swing.JCheckBox;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.Dimension;
import javax.swing.AbstractListModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BComp extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtBtn;
	private JPasswordField pwdBtn;
	private JList list1;
	private JList list2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BComp dialog = new BComp();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BComp() {
		setTitle(" :: 기본 컴퍼넌트 :: ");
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JButton imgbtn = new JButton("이미지 버튼");
			imgbtn.setToolTipText("이미지버튼입니다");
			imgbtn.setFont(new Font("맑은 고딕", Font.BOLD, 14));
			imgbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			imgbtn.setIcon(new ImageIcon(BComp.class.getResource("/marvin/swing/images/icon/icon.png")));
			contentPanel.add(imgbtn);
		}
		{
			JLabel imglabel = new JLabel("이미지레이블");
			imglabel.setIcon(new ImageIcon(BComp.class.getResource("/marvin/swing/images/icon/icon2.png")));
			imglabel.setFont(new Font("맑은 고딕", Font.BOLD, 14));
			contentPanel.add(imglabel);
		}
		{
			JCheckBox chkSuji = new JCheckBox("수지");
			chkSuji.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if ( c.isSelected() )
						txtBtn.setText( c.getText() );
					else 
						txtBtn.setText( "" );

				}
			});
			contentPanel.add(chkSuji);
		}
		{
			JCheckBox chkDanbi = new JCheckBox("단비");
			chkDanbi.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JCheckBox c = (JCheckBox) e.getSource();
					if ( c.isSelected() )
						txtBtn.setText( c.getText() );
					else 
						txtBtn.setText( "" );
						
				}
			});
			contentPanel.add(chkDanbi);
		}
		{JCheckBox chkHyoju = new JCheckBox("효주");
		chkHyoju.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JCheckBox c = (JCheckBox) e.getSource();
				if ( c.isSelected() )
					txtBtn.setText( c.getText() );
				else 
					txtBtn.setText( "" );
					
			}
		});
			contentPanel.add(chkHyoju);}
		
		
		// 라디오버튼 선언 시작
		JRadioButton radSuji = new JRadioButton("수지");
		radSuji.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JRadioButton r = (JRadioButton) e.getSource();
				if ( r.isSelected() )	//선택되면
					txtBtn.setText(r.getText());
				else
					txtBtn.setText("");
					
			}
		});
			contentPanel.add(radSuji);
		
		JRadioButton radDanbi = new JRadioButton("단비");
		radDanbi.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JRadioButton r = (JRadioButton) e.getSource();
				if ( r.isSelected() )	//선택되면
					txtBtn.setText(r.getText());
				else
					txtBtn.setText("");
				
				
			}
		});
			contentPanel.add(radDanbi);
		
		JRadioButton radHyoju = new JRadioButton("효주");
		radHyoju.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JRadioButton r = (JRadioButton) e.getSource();
				if ( r.isSelected() )	//선택되면
					txtBtn.setText(r.getText());
				else
					txtBtn.setText("");
				
			}
		});
			contentPanel.add(radHyoju);
		// 라디오버튼 선언 끝
		// 라디오버튼 그룹 생성 및 추가
		ButtonGroup rgroup = new ButtonGroup();
		rgroup.add(radSuji);
		rgroup.add(radDanbi);
		rgroup.add(radHyoju);
		{
			txtBtn = new JTextField();
			contentPanel.add(txtBtn);
			txtBtn.setColumns(10);
		}
		{
			JButton tx1Btn = new JButton("입력문자보기");
			tx1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null,	txtBtn.getText());
					// textField에 입력된 문자 출력
					
				}
			});
			contentPanel.add(tx1Btn);
		}
		{
			pwdBtn = new JPasswordField();
			pwdBtn.setEchoChar('ㅹ');
			pwdBtn.setColumns(16);
			contentPanel.add(pwdBtn);
		}
		{
			JButton pwd1Btn = new JButton("입력문자보기");
			pwd1Btn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null,	pwdBtn.getText());
					// pwdfield에 입력된 문자 출력
					// pwd.getPassword랑 비교
					
				}
			});
			contentPanel.add(pwd1Btn);
		}
		{
			JScrollPane scrollPane1 = new JScrollPane();
			scrollPane1.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane1);
			{
				// Jlist에 추가할 항목을 배열로 생성
				String[] data = {"수지","단비","효주","지현","지민","현진","태연"};
				list1 = new JList(data);
				scrollPane1.setViewportView(list1);
			}
		}
		{
			JScrollPane scrollPane2 = new JScrollPane();
			scrollPane2.setPreferredSize(new Dimension(150, 100));
			contentPanel.add(scrollPane2);
			{
				list2 = new JList();
				list2.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						if ( e.getClickCount() == 2){
							JOptionPane.showMessageDialog(null, list2.getSelectedIndex()+ "/" 
															+ list2.getSelectedValue());
						}
					}
				});
				list2.setModel(new AbstractListModel() {
					String[] values = new String[] {"구", "지매", "성계", "각대", "구", "재미", "지완", "봉이","푼이","혜선","장생"};
					public int getSize() {
						return values.length;
					}
					public Object getElementAt(int index) {
						return values[index];
					}
				});
				scrollPane2.setViewportView(list2);
			}
		}
		{
			JButton listbtn = new JButton("리스트 선택보기");
			listbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, list1.getSelectedIndex() + "/"
													+ list1.getSelectedValue());
					
				}
			});
			contentPanel.add(listbtn);
		}
		{
			JScrollPane scrollPane3 = new JScrollPane();
			scrollPane3.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane3.setPreferredSize(new Dimension(200, 150));
			contentPanel.add(scrollPane3);
			{
				JTextArea textArea = new JTextArea();
				textArea.setColumns(30);
				scrollPane3.setViewportView(textArea);
			}
		}
		{
			String[] data = { " Jetta "," Golf"," Mini Cooper ", " Audi A4 "," Benz Cla " };
			
			final JComboBox comboBox1 = new JComboBox(data);
			comboBox1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					JOptionPane.showMessageDialog(null, comboBox1.getSelectedIndex()+ "/"
													+ comboBox1.getSelectedItem());
				}
			});
			contentPanel.add(comboBox1);
		}
		{
			JComboBox comboBox2 = new JComboBox();
			comboBox2.setModel(new DefaultComboBoxModel(new String[] {"크루즈", "아반떼", "K3", "SM3", "코란도C"}));
			contentPanel.add(comboBox2);
		}
		// 라디오버튼 그룹 생성 및 추가 끝
		
		
			
			
			
			
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("OK");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
		}
	}

}
